﻿package mortal.game.view.copy.ChallengeCopy.newView
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import fl.controls.*;
    import fl.data.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.mvc.interfaces.*;

    public class ChallengeMopResutlWin extends BaseWindow
    {
        private var _rewardRenderList:GTileList;
        private var _allRewardList:GTileList;
        private var _todayNum:GTextFiled;
        private var _exp:GTextFiled;
        private var _money:GTextFiled;
        private var _btnFinish:GButton;
        private var _txtModding:GTextFiled;
        private var _rewardList:Array;
        private var _itemDic:Dictionary;
        private var _mopRewards:Array;
        private var _data:SChallengeMopRewardMsg;
        private var _timeId:int;
        private var _curIndex:int = 0;
        private var _addExp:int = 0;
        private var _addMoney:int = 0;
        private static var _instance:ChallengeMopResutlWin;

        public function ChallengeMopResutlWin(param1:ILayer = null)
        {
            this._rewardList = new Array();
            this._itemDic = new Dictionary();
            this._mopRewards = new Array();
            if (_instance != null)
            {
                throw new Error(" ChallengeMopResutlWin 单例 ");
            }
            setSize(580, 480);
            this.title = Language.getString(35120);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.bg(5, 34, 356, 421, this, ImagesConst.PanelBg));
            this.pushUIToDisposeVec(UIFactory.bg(365, 34, 212, 421, this, ImagesConst.PanelBg));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.challengeCopyNewTxt_7, 442, 40, this));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(35121), 435, 100, 150, 20, this));
            var _loc_1:* = UIFactory.gBitmap(ImagesConst.GroupCopySide, 355, 108, this);
            var _loc_2:* = UIFactory.gBitmap(ImagesConst.GroupCopySide, 591, 108, this);
            _loc_2.scaleX = -1;
            this.pushUIToDisposeVec(_loc_1);
            this.pushUIToDisposeVec(_loc_2);
            this._todayNum = UIFactory.gTextField(Language.getString(35122), 380, 65, 220, 20, this);
            this._exp = UIFactory.gTextField(Language.getString(35123), 380, 380, 200, 20, this);
            this._money = UIFactory.gTextField(Language.getString(35124), 380, 420, 200, 20, this);
            this._rewardRenderList = UIFactory.tileList(10, 40, 330, 400, this);
            this._rewardRenderList.columnWidth = 330;
            this._rewardRenderList.rowHeight = 130;
            this._rewardRenderList.horizontalGap = -1;
            this._rewardRenderList.verticalGap = 0;
            this._rewardRenderList.setStyle("cellRenderer", ChallengeMopRender);
            this._rewardRenderList.direction = ScrollBarDirection.VERTICAL;
            this._rewardRenderList.verticalScrollPolicy = ScrollPolicy.ON;
            this._allRewardList = UIFactory.tileList(380, 126, 200, 300, this);
            this._allRewardList.columnWidth = 46;
            this._allRewardList.rowHeight = 46;
            this._allRewardList.horizontalGap = 3;
            this._allRewardList.verticalGap = 3;
            this._allRewardList.selectable = false;
            this._allRewardList.setStyle("cellRenderer", ItemCellRenderer);
            this._txtModding = UIFactory.gTextField(Language.getString(35125), 273, 458, 100, 20, this, GlobalStyle.textFormatHuang2);
            this._btnFinish = UIFactory.gButton(Language.getString(35126), 259, 455, 70, 28, this);
            this._btnFinish.configEventListener(MouseEvent.CLICK, closeBtnClickHandler);
            this._btnFinish.visible = false;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            clearTimeout(this._timeId);
            this._rewardRenderList.dispose(param1);
            this._rewardRenderList = null;
            this._todayNum.dispose(param1);
            this._todayNum = null;
            this._allRewardList.dispose(param1);
            this._allRewardList = null;
            this._exp.dispose(param1);
            this._exp = null;
            this._money.dispose(param1);
            this._money = null;
            this._btnFinish.dispose(param1);
            this._btnFinish = null;
            this._txtModding.dispose(param1);
            this._txtModding = null;
            this._data = null;
            this._rewardList.length = 0;
            this._mopRewards.length = 0;
            this._itemDic = new Dictionary();
            super.disposeImpl(param1);
            return;
        }// end function

        public function set data(param1:SChallengeMopRewardMsg) : void
        {
            this._data = param1;
            this._curIndex = 0;
            this._addExp = 0;
            this._addMoney = 0;
            this.createList();
            this.refreshDate();
            return;
        }// end function

        private function createList() : void
        {
            var _loc_1:Object = null;
            var _loc_2:String = null;
            this._mopRewards.length = 0;
            for (_loc_2 in this._data.mopRewards)
            {
                
                _loc_1 = {arr:this._data.mopRewards[_loc_2], section:int(_loc_2)};
                this._mopRewards.push(_loc_1);
            }
            this._mopRewards.sort(this.sortItem);
            return;
        }// end function

        private function sortItem(param1:Object, param2:Object) : int
        {
            if (param1.section < param2.section)
            {
                return -1;
            }
            return 1;
        }// end function

        private function refreshDate() : void
        {
            var _loc_1:SChallengeMopReward = null;
            var _loc_2:Array = null;
            var _loc_3:Object = null;
            if (this._mopRewards[this._curIndex])
            {
                _loc_2 = this._mopRewards[this._curIndex].arr;
                if (_loc_2.length == 0)
                {
                    var _loc_4:String = this;
                    var _loc_5:* = this._curIndex + 1;
                    _loc_4._curIndex = _loc_5;
                    this.refreshDate();
                    return;
                }
                _loc_1 = _loc_2.shift();
                _loc_3 = {info:_loc_1, section:this._mopRewards[this._curIndex].section};
                this._rewardList.push(_loc_3);
            }
            else
            {
                clearTimeout(this._timeId);
                this._btnFinish.visible = true;
                this._txtModding.visible = false;
                return;
            }
            this._rewardRenderList.dataProvider = this.getRewardList();
            this._rewardRenderList.scrollToIndex(this._rewardRenderList.dataProvider.length);
            this._allRewardList.dataProvider = this.getAllRewarlist();
            this._addExp = this._addExp + _loc_1.exp;
            if (this._addExp > 0)
            {
                this._exp.text = Language.getString(35123) + this._addExp;
            }
            else
            {
                this._exp.text = "";
            }
            this._addMoney = this._addMoney + 0;
            if (this._addMoney > 0)
            {
                this._money.text = Language.getString(35124) + this._addExp;
            }
            else
            {
                this._money.text = "";
            }
            this._timeId = setTimeout(this.refreshDate, 500);
            return;
        }// end function

        private function getRewardList() : DataProvider
        {
            var _loc_2:Object = null;
            var _loc_3:SChallengeMopReward = null;
            var _loc_4:TChallengeCopy = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_1:* = new DataProvider();
            while (_loc_6 < this._rewardList.length)
            {
                
                _loc_3 = this._rewardList[_loc_6].info;
                _loc_5 = this._rewardList[_loc_6].section;
                _loc_4 = ChallengeCopyConfig.instance.getDataByTowerAndSectionAndDif(Cache.instance.copy.challengCopy.mopTower, _loc_5, _loc_6 % 7 + 1);
                _loc_2 = {info:_loc_3, tchallenge:_loc_4};
                _loc_1.addItem(_loc_2);
                _loc_6++;
            }
            return _loc_1;
        }// end function

        private function getAllRewarlist() : DataProvider
        {
            var _loc_2:ItemData = null;
            var _loc_3:SChallengeMopReward = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:String = null;
            var _loc_7:int = 0;
            var _loc_8:SReward = null;
            var _loc_1:* = new DataProvider();
            this._itemDic = new Dictionary();
            while (_loc_5 < this._rewardList.length)
            {
                
                _loc_3 = this._rewardList[_loc_5].info;
                _loc_4 = _loc_3.rewards;
                for each (_loc_8 in _loc_4)
                {
                    
                    if (this._itemDic[_loc_8.code] == null)
                    {
                        this._itemDic[_loc_8.code] = _loc_8.num;
                        continue;
                    }
                    this._itemDic[_loc_8.code] = this._itemDic[_loc_8.code] + _loc_8.num;
                }
                _loc_5++;
            }
            _loc_4 = new Array();
            for (_loc_6 in this._itemDic)
            {
                
                _loc_2 = new ItemData(int(_loc_6));
                _loc_2.serverData = new SPlayerItem();
                _loc_2.serverData.itemCode = int(_loc_6);
                _loc_2.itemAmount = this._itemDic[_loc_6];
                _loc_4.push(_loc_2);
            }
            while (_loc_7 < 20)
            {
                
                _loc_2 = _loc_4[_loc_7];
                _loc_1.addItem(_loc_2);
                _loc_7++;
            }
            return _loc_1;
        }// end function

        public static function get instance() : ChallengeMopResutlWin
        {
            if (_instance == null)
            {
                _instance = new ChallengeMopResutlWin;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
